﻿using ChronoScript_Compiler.Language;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Viper.Opcodes;

namespace ChronoScript_Compiler.CodeGen
{
    public partial class CodeGen
    {
        public void GetBooleanValue(LanguageDefinition.Expression exp)
        {
            if (GuessType(exp) == VType.Dword)
                ParseArthemeticExpression(exp);
            else if (GuessType(exp) == VType.String)
                ParseStringExpression(exp);
        }
        public void ParseBooleanExpression(LanguageDefinition.Expression exp)
        {
            foreach (LanguageDefinition.SyntaxNode sn in exp.Items)
            {
                if (sn is LanguageDefinition.Operator)
                {
                    LanguageDefinition.Operator op = sn as LanguageDefinition.Operator;
                    if (sn is LanguageDefinition.And)
                    {
                        ParseBooleanExpression((LanguageDefinition.Expression)op.Op1);
                        ParseBooleanExpression((LanguageDefinition.Expression)op.Op2);
                        Assembler.Emit(new and());

                    }
                    else
                    {
                        GetBooleanValue((LanguageDefinition.Expression)op.Op1);
                        GetBooleanValue((LanguageDefinition.Expression)op.Op2);
                        if (sn is LanguageDefinition.Equal)
                        {
                            if (GuessType((LanguageDefinition.Expression)op.Op1) == VType.Dword)
                            {
                                Assembler.Emit(new teq());
                            }
                        }
                        else if (sn is LanguageDefinition.NotEqual)
                        {
                            if (GuessType((LanguageDefinition.Expression)op.Op1) == VType.Dword)
                            {
                                Assembler.Emit(new tne());
                            }
                        }
                        else if (sn is LanguageDefinition.GreaterThan)
                        {
                            if (GuessType((LanguageDefinition.Expression)op.Op1) == VType.Dword)
                            {
                                Assembler.Emit(new tgt());
                            }
                        }
                        else if (sn is LanguageDefinition.LessThan)
                        {
                            if (GuessType((LanguageDefinition.Expression)op.Op1) == VType.Dword)
                            {
                                Assembler.Emit(new tlt());
                            }
                        }
                    }
                }
            }
        }

    }
}
